package it.unipr.ce.dsg.s2p.example.peerdroid;


import org.zoolu.tools.Log;

import npu.database.*;

import it.unipr.ce.dsg.s2p.util.FileHandler;
import android.app.Activity;
import android.app.TabActivity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;



public class PeerActivity extends TabActivity {
	
//	private EditText addressEdit;
//	private Button sendBut;
//	private TextView addressPeer;
//	private Button clearBut;
//	private TextView contactAddressPeer;
//	private Button closeBut;

	private SQLiteAdapter mySQLiteAdapter;
	//private static final int MENU_CONFIG = 1;
	private static final int MENU_ADDCONTACT = 1;
//	private static final int MENU_BOOT = 2;
	private static final int MENU_VIEWCONTACT = 2;
	private static final int MENU_LIST = 3;
	private static final int MENU_SBC = 4;
	
	private static final int DIALOG_CONFIG = 5;
	private static final int DIALOG_BOOTSTRAP = 6;
	
	private static final int ACTIVITY_PEER_LIST=7;
	
	public static Handler handler = new Handler();

	
	public static SimplePeer peer = null;
//	private Log log;
//	private FileHandler fileHandler;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		 mySQLiteAdapter = new SQLiteAdapter(this);

		TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
		TabSpec tabsend = tabHost.newTabSpec("tid1");
		TabSpec tabconfig = tabHost.newTabSpec("tid1");
		TabSpec tabjoin = tabHost.newTabSpec("tid1");
		TabSpec tablist = tabHost.newTabSpec("tid1");
		/** TabSpec setIndicator() is used to set name for the tab. */
		/** TabSpec setContent() is used to set content for a  particular tab. */
		tabsend.setIndicator("Send",getResources().getDrawable(R.drawable.iconsend)).setContent(new  Intent(this,Send.class));
		tabconfig.setIndicator("Configure",getResources().getDrawable(R.drawable.iconconfig)).setContent(new  Intent(this,My_config_peer.class));
		tabjoin.setIndicator("Join Bootstrap",getResources().getDrawable(R.drawable.iconjoin)).setContent(new  Intent(this,My_join_peer.class));
		tablist.setIndicator("List",getResources().getDrawable(R.drawable.iconlist)).setContent(new  Intent(this,My_peer_list.class));
		/** Add tabSpec to the TabHost to display. */
		tabHost.addTab(tabsend);
		tabHost.addTab(tabconfig);
		tabHost.addTab(tabjoin);
		tabHost.addTab(tablist);
		tabHost.setCurrentTab(0);//display tabsend as default
		
//		addressPeer = (TextView)findViewById(R.id.addressPeer);
//		contactAddressPeer = (TextView)findViewById(R.id.contAddressPeer);
//		addressEdit = (EditText)findViewById(R.id.addressEditor);
//		
//		sendBut = (Button)findViewById(R.id.sendPing);
//		clearBut = (Button)findViewById(R.id.clearButton);
//		closeBut = (Button)findViewById(R.id.closeApp);
		
//		sendBut.setOnClickListener(new View.OnClickListener() {
//
//			@Override
//			public void onClick(View arg0) {
//
//				if(peer!=null) {
//					if(addressEdit.getText().toString().contentEquals("peer@pcpeer.com:port")){
//						Toast toast = Toast.makeText(getApplicationContext(),"Please type a Peer address (ex. bob@192.168.1.100:5070)" ,Toast.LENGTH_LONG);
//	         		 	toast.show();	
//					}
//					else{
//						peer.pingToPeer(addressEdit.getText().toString());
//						addressEdit.setText("");		
//					}	
//				}
//				else
//					showDialog(DIALOG_CONFIG);
//
//			}
//		});
//		
//		clearBut.setOnClickListener(new View.OnClickListener() {
//
//			@Override
//			public void onClick(View arg0) {
//
//				addressEdit.setText("");
//
//			}
//		});
//		
//		closeBut.setOnClickListener(new View.OnClickListener() {
//
//			@Override
//			public void onClick(View arg0) {
//
//				peer.halt();
//				finish();
//
//			}
//		});
//
	}//end onCreate
	
	//menu handle
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		boolean result = super.onCreateOptionsMenu(menu);
		//create menu
		MenuItem addcontact = menu.add(Menu.NONE, MENU_ADDCONTACT, 1, "Add Contact");
		addcontact.setIcon(R.drawable.config);
		MenuItem viewcontact = menu.add(Menu.NONE, MENU_VIEWCONTACT, 2, "View Contact");
		viewcontact.setIcon(R.drawable.boot);
		
//		MenuItem peerlist = menu.add(Menu.NONE, MENU_LIST, 3, "Peer List");
//		peerlist.setIcon(R.drawable.list);
//		MenuItem sbc = menu.add(Menu.NONE, MENU_SBC, 4, "SBC");
//		sbc.setIcon(R.drawable.sbc);

		return result;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		//select menu
		int id = item.getItemId();
		switch(id){
		case MENU_ADDCONTACT:
			addContact();
			return true;
		case MENU_VIEWCONTACT:
			viewContact();
			return true;
//		case MENU_LIST:
//			viewPeerList();
//			return true;
//		case MENU_SBC:
//			contactSBC();
//			return true;	

		}

		return super.onOptionsItemSelected(item);
	}
	//////end Menu Handle

	private void init(String name){
	
		
//		if(nodeConfig.log_path!=null){
//
//			fileHandler = new FileHandler();
//
//			if(!fileHandler.isDirectoryExists(nodeConfig.log_path))
//				fileHandler.createDirectory(nodeConfig.log_path);
//
//			log = new Log(nodeConfig.log_path+"info_"+peerDescriptor.getAddress()+".log", Log.LEVEL_MEDIUM);
//		}
		
		peer = new SimplePeer(null, "4654amv65d4as4d65a3", name,  5554);
		peer.setPeerActivity(this);
		
		
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		if(peer==null){
			init("A");
//			addressPeer.setText("Address: "+peer.getAddressPeer());
		//	addressPeer.setText("Address: "+peer.getAddressPeer()+peer.getBootstrapPeer()+"aa");
		}
	}
//
//	
//	
//
	@Override
	protected void onResume() {
		
		super.onResume();
//		addressPeer.setText("Address: "+peer.getAddressPeer());
//		contactAddressPeer.setText("Contact Address: "+peer.getContactAddressPeer());
		
	}
//
//
//

	
	
//
//	private void contactSBC() {
//		
//		peer.contactSBC();
//
//	}
//
//	private void viewPeerList() {
//		
//		//call activity PeerList
//		Intent intent = new Intent(this, PeerListActivity.class);
//		startActivityForResult(intent, ACTIVITY_PEER_LIST);
//	}
//
//	private void contactBootstrap() {
//
//		if(peer.getBootstrapPeer()!=null){
//			Intent intent = new Intent(this, JoinPeerActivity.class);
//			startActivity(intent);
//		}
//		else
//			showDialog(DIALOG_CONFIG);
//	}
//
	private void addContact() {

		//call activity ConfigPeer
		Intent intent = new Intent(this, Manual_add_contact.class);
		startActivity(intent);

	}
	
	private void viewContact() {

		//call activity ConfigPeer
		Intent intent = new Intent(this, View_contact.class);
		startActivity(intent);

	}
//	
//
//
//	@Override
//	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//		
//		super.onActivityResult(requestCode, resultCode, data);
//		
//		Bundle extras = data.getExtras();
//		 
//		switch(requestCode) {
//        case ACTIVITY_PEER_LIST:
//        	String peerAddress = extras.getString(PeerListActivity.PEER_ADDRESS);
//        	addressEdit.setText(peerAddress);
//        	break;
//		}
//        	
//	}
//
//
//
//	@Override
//	protected Dialog onCreateDialog(int id) {
//		Dialog dialog;
//		
//	    switch(id) {
//	    case DIALOG_CONFIG:
//	    	AlertDialog.Builder builderConf = new AlertDialog.Builder(this);
//	    	builderConf.setMessage("Set peer configuration!")
//	    			.setTitle("Warning")
//	    	       .setCancelable(false)
//	    	       .setPositiveButton("Close", new DialogInterface.OnClickListener() {
//	    	           public void onClick(DialogInterface dialog, int id) {
//	    	                dismissDialog(DIALOG_CONFIG);
//	    	           }
//	    	       });
//	    	dialog = builderConf.create();
//	        break;
//	    case DIALOG_BOOTSTRAP:
//	    	AlertDialog.Builder builderBoot = new AlertDialog.Builder(this);
//	    	builderBoot.setMessage("Set the address of BootstrapPeer in the Configuration section!")
//	    			.setTitle("Warning")
//	    	       .setCancelable(false)
//	    	       .setPositiveButton("Close", new DialogInterface.OnClickListener() {
//	    	           public void onClick(DialogInterface dialog, int id) {
//	    	                dismissDialog(DIALOG_BOOTSTRAP);
//	    	           }
//	    	       });
//	    	dialog = builderBoot.create();
//	        break;
//	    default:
//	        dialog = null;
//	    }
//	    return dialog;
//
//		
//	}
//	
	
}
